{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\" style=\"margin-top: 1em;\"><ul class=\"toc-item\"></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TensorBoard and tfruns in R <a class=\"tocSkip\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TensorBoard in R"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# example from https://keras.rstudio.com/articles/examples/mnist_mlp.html\n",
    "\n",
    "library(keras)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define hyperparameters -----------\n",
    "\n",
    "batch_size <- 128\n",
    "num_classes <- 10\n",
    "epochs <- 30"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "60000 train samples\n",
      "10000 test samples\n"
     ]
    }
   ],
   "source": [
    "# Data Preparation -----------\n",
    "\n",
    "# The data, shuffled and split between train and test sets\n",
    "c(c(x_train, y_train), c(x_test, y_test)) %<-% dataset_mnist()\n",
    "\n",
    "x_train <- array_reshape(x_train, c(nrow(x_train), 784))\n",
    "x_test <- array_reshape(x_test, c(nrow(x_test), 784))\n",
    "\n",
    "# Transform RGB values into [0,1] range\n",
    "x_train <- x_train / 255\n",
    "x_test <- x_test / 255\n",
    "\n",
    "cat(nrow(x_train), 'train samples\\n')\n",
    "cat(nrow(x_test), 'test samples\\n')\n",
    "\n",
    "# Convert class vectors to binary class matrices\n",
    "y_train <- to_categorical(y_train, num_classes)\n",
    "y_test <- to_categorical(y_test, num_classes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "________________________________________________________________________________\n",
      "Layer (type)                        Output Shape                    Param #     \n",
      "================================================================================\n",
      "dense_1 (Dense)                     (None, 256)                     200960      \n",
      "________________________________________________________________________________\n",
      "dropout_1 (Dropout)                 (None, 256)                     0           \n",
      "________________________________________________________________________________\n",
      "dense_2 (Dense)                     (None, 128)                     32896       \n",
      "________________________________________________________________________________\n",
      "dropout_2 (Dropout)                 (None, 128)                     0           \n",
      "________________________________________________________________________________\n",
      "dense_3 (Dense)                     (None, 10)                      1290        \n",
      "================================================================================\n",
      "Total params: 235,146\n",
      "Trainable params: 235,146\n",
      "Non-trainable params: 0\n",
      "________________________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "# Define Model ---------------\n",
    "\n",
    "model <- keras_model_sequential()\n",
    "model %>% \n",
    "  layer_dense(units = 256, activation = 'relu', input_shape = c(784)) %>% \n",
    "  layer_dropout(rate = 0.4) %>% \n",
    "  layer_dense(units = 128, activation = 'relu') %>%\n",
    "  layer_dropout(rate = 0.3) %>%\n",
    "  layer_dense(units = 10, activation = 'softmax')\n",
    "\n",
    "summary(model)\n",
    "\n",
    "model %>% compile(\n",
    "  loss = 'categorical_crossentropy',\n",
    "  optimizer = optimizer_rmsprop(),\n",
    "  metrics = c('accuracy')\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Started TensorBoard at http://127.0.0.1:4826 \n"
     ]
    }
   ],
   "source": [
    "# Training the model --------\n",
    "tensorboard(\"logs\")\n",
    "\n",
    "history <- model %>% fit(\n",
    "  x_train, y_train,\n",
    "  batch_size = batch_size,\n",
    "  epochs = epochs,\n",
    "  verbose = 1,\n",
    "  validation_split = 0.2,\n",
    "  callbacks = callback_tensorboard(\"logs\")\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# tfruns in R"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using run directory runs/2017-12-23T04-59-04Z\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "> library(keras)\n",
      "\n",
      "> FLAGS <- flags(flag_numeric(\"dropout1\", 0.4), flag_numeric(\"dropout2\", \n",
      "+     0.3))\n",
      "\n",
      "> mnist <- dataset_mnist()\n",
      "\n",
      "> x_train <- mnist$train$x\n",
      "\n",
      "> y_train <- mnist$train$y\n",
      "\n",
      "> x_test <- mnist$test$x\n",
      "\n",
      "> y_test <- mnist$test$y\n",
      "\n",
      "> dim(x_train) <- c(nrow(x_train), 784)\n",
      "\n",
      "> dim(x_test) <- c(nrow(x_test), 784)\n",
      "\n",
      "> x_train <- x_train/255\n",
      "\n",
      "> x_test <- x_test/255\n",
      "\n",
      "> y_train <- to_categorical(y_train, 10)\n",
      "\n",
      "> y_test <- to_categorical(y_test, 10)\n",
      "\n",
      "> model <- keras_model_sequential()\n",
      "\n",
      "> model %>% layer_dense(units = 256, activation = \"relu\", \n",
      "+     input_shape = c(784)) %>% layer_dropout(rate = FLAGS$dropout1) %>% \n",
      "+     layer_dense .... [TRUNCATED] \n",
      "\n",
      "> model %>% compile(loss = \"categorical_crossentropy\", \n",
      "+     optimizer = optimizer_rmsprop(lr = 0.001), metrics = c(\"accuracy\"))\n",
      "\n",
      "> history <- model %>% fit(x_train, y_train, batch_size = 128, \n",
      "+     epochs = 20, verbose = 1, validation_split = 0.2)\n",
      "\n",
      "> plot(history)\n",
      "\n",
      "> score <- model %>% evaluate(x_test, y_test, verbose = 0)\n",
      "\n",
      "> cat(\"Test loss:\", score$loss, \"\\n\")\n",
      "Test loss: 0.09684165 \n",
      "\n",
      "> cat(\"Test accuracy:\", score$acc, \"\\n\")\n",
      "Test accuracy: 0.9808 \n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "Run completed: runs/2017-12-23T04-59-04Z\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# run these in the R Studio for seeing the visualization window\n",
    "library(tfruns)\n",
    "training_run(\"ch-17_mnist_mlp.R\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R 3.4.2",
   "language": "R",
   "name": "ir342"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "3.4.3"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": true,
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
